package com.atXiao.food.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BaseRespEnum;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.dao.RecipecategoryDao;
import com.atXiao.food.entity.RecipecategoryEntity;
import com.atXiao.food.reqbody.category.CategoryVo;
import com.atXiao.food.rspbody.RspBody;
import com.atXiao.food.rspbody.category.CategoryItem;
import com.atXiao.food.rspcode.BaseResultCode;
import com.atXiao.food.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lixiaoxiao
 * @date: 2023/11/23 10:08
 * @version: 1.0
 * @desc: 首页菜谱业务实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    public final static Log logger = new Log(CategoryServiceImpl.class);

    @Autowired
    RecipecategoryDao recipecategoryDao;

    @Override
    public List<CategoryItem> getRecipeCategoryList() {
        // 查询列表数据
        List<RecipecategoryEntity> categoryList = recipecategoryDao.selectList(null);
        List<CategoryItem> itemList = new ArrayList<>();
        // 组装数据
        if (!CollectionUtils.isEmpty(categoryList)) {
            itemList = categoryList.stream().map(item -> {
                CategoryItem categoryItem = new CategoryItem();
                // 属性拷贝
                BeanUtils.copyProperties(item, categoryItem);
                return categoryItem;
            }).collect(Collectors.toList());
        }

        logger.info("查询分类列表数据: " + JSONObject.toJSONString(categoryList));
        return itemList;
    }

    @Override
    public RspBody addCategory(String trace, CategoryVo categoryVo) {
        // 首页分类名称
        String categoryName = categoryVo.getCategoryName();
        // 首页分类图片
        String categoryImg = categoryVo.getCategoryImg();
        QueryWrapper<RecipecategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name", categoryName);
        RecipecategoryEntity categoryItem = recipecategoryDao.selectOne(queryWrapper);
        // 校验失败
        if (!ObjectUtils.isEmpty(categoryItem)) {
            RspBody<Object> rspBody = new RspBody<>();
            logger.info(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.ADD_CATEGORY_ITEM, "已经存在相同名称的分类信息");
            rspBody.setRetCode(BaseRespEnum.CATEGORY_NOT_NULL.getCode());
            rspBody.setRetDesc(BaseRespEnum.CATEGORY_NOT_NULL.getMsg());
            return rspBody;
        }
        // 数据库实体
        RecipecategoryEntity recipecategoryEntity = new RecipecategoryEntity();
        BeanUtils.copyProperties(categoryVo, recipecategoryEntity);
        recipecategoryDao.insert(recipecategoryEntity);
        logger.info(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.ADD_CATEGORY_ITEM, "食谱分类保存成功" + JSONObject.toJSONString(recipecategoryEntity));
        return RspBody.ok(null);
    }

    @Override
    public RspBody deleteCategory(String trace, CategoryVo categoryVo) {
        return null;
    }

    @Override
    public RspBody updateCategory(String trace, CategoryVo categoryVo) {
        return null;

    }
}
